<script>
    //初始化对象
    editor = ace.edit("code");
    //设置风格和语言（更多风格和语言，请到github上相应目录查看）
    theme = "github";
    language = "sh";
    editor.setTheme("ace/theme/" + theme);
    editor.session.setMode("ace/mode/" + language);
    editor.getSession().setTabSize(4);
    editor.getSession().setUseWrapMode(true);

    //字体大小
    editor.setFontSize(14);

    //设置只读（true时只读，用于展示代码）
    editor.setReadOnly(false);

    //自动换行,设置为off关闭
    editor.setOption("wrap", "off");

    //启用提示菜单
    ace.require("ace/ext/language_tools");
    editor.setOptions({
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true
    });


    $("#add-sub").click(function () {
        var script_name = $("#script-name").val();
        var script_msg = $("#script-msg").val();
        var script_arg = $("#script-arg").val();
        var script_text = editor.getValue();
        if (check_str(script_arg)) {
            $.ajax({
                type: "POST",
                async: false,
                url: "/ops/addscript/",
                data: {
                    "script_name": script_name,
                    "script_msg": script_msg,
                    "script_arg": script_arg,
                    "script_text": script_text
                },
                dataType: "json",
                success: function (data) {
                    $("#addModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        } else {
            spop({
                template: "参数格式不正确",
                style: 'error',
                autoclose: 2000
            });
        }
    });


    //初始化对象
    editor1 = ace.edit("edit-code");
    //设置风格和语言（更多风格和语言，请到github上相应目录查看）
    theme = "github";
    language = "sh";
    editor1.setTheme("ace/theme/" + theme);
    editor1.session.setMode("ace/mode/" + language);
    editor1.getSession().setTabSize(4);
    editor1.getSession().setUseWrapMode(true);

    //字体大小
    editor1.setFontSize(14);

    //设置只读（true时只读，用于展示代码）
    editor1.setReadOnly(false);

    //自动换行,设置为off关闭
    editor1.setOption("wrap", "off");

    //启用提示菜单
    ace.require("ace/ext/language_tools");
    editor1.setOptions({
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true
    });

    function check_str(str) {
        var objRegExp = /\s+|\;+|\&+|\|+/g;
        if (objRegExp.test(str)) {
            return false
        } else {
            return true;
        }
    }

    //获取修改内容
    $("td>a[name='edit']").click(function () {
        var edit_id = $(this).attr("edit_id");
        $.ajax({
            type: "POST",
            async: false,
            url: "/ops/idscript/",
            data: {"edit_id": edit_id},
            dataType: "json",
            success: function (data) {
                $("#edit-script-name").val(data.ScriptName);
                $("#edit-script-arg").val(data.ScriptArg);
                $("#edit-script-msg").val(data.ScriptMsg);
                editor1.setValue(data.ScriptText);
                $("#edit-sub").attr('edit_id', data.ID);
                $("#editModal").modal("show");
            },
            error: function (data) {
                spop({
                    template: data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        });
    });

    //修改
    $("#edit-sub").click(function () {
        var edit_id = $(this).attr("edit_id");
        var script_name = $("#edit-script-name").val();
        var script_msg = $("#edit-script-msg").val();
        var script_arg = $("#edit-script-arg").val();
        var script_text = editor1.getValue();
        if (check_str(script_arg)) {
            $.ajax({
                type: "POST",
                async: false,
                url: "/ops/editscript/",
                data: {
                    "edit_id": edit_id,
                    "script_name": script_name,
                    "script_msg": script_msg,
                    "script_arg": script_arg,
                    "script_text": script_text
                },
                dataType: "json",
                success: function (data) {
                    $("#editModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        } else {
            spop({
                template: "参数格式不正确",
                style: 'error',
                autoclose: 2000
            });
        }
    });

    //删除
    $("td>a[name='del']").click(function () {
        var del_id = $(this).attr('del_id');
        var statu = confirm("是否确认删除!");
        if (statu == true) {
            $.ajax({
                type: "post",
                async: false,
                url: "/ops/delscript/",
                data: {"del_id": del_id},
                dataType: "json",
                success: function (data) {
                    spop({
                        template: data,
                        style: 'warning',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        }
    });

    var setting = {
        view: {
            selectedMulti: false,
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: false
        }
    };

    var zNodes = {{ .ztree_data}};
    $(document).ready(function () {
        treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });

    //ip地址正则匹配
    function isValidIP(ip) {
        var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
        return reg.test(ip);
    }

    function getIpList() {
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);
        var ip_list = "";
        for (var i = 0; i < nodes.length; i++) {
            var ip_name = nodes[i].name;
            ip = ip_name.split("_")[0];
            if (isValidIP(ip)) {
                ip_list += ip + ",";
            }
        }
        return ip_list
    }

    $("td>a[name='exec']").click(function () {
        var exec_id = $(this).attr("exec_id");
        $("#execModal").modal("show");
        $("#exec-sub").attr("exec_id", exec_id);
    });

    $("#exec-sub").click(function () {
        var exec_id = $(this).attr("exec_id");
        var ip_list = getIpList();
        if (ip_list != "") {
            $.ajax({
                type: "post",
                async: false,
                url: "/ops/deployscript/",
                data: {"ip_list": ip_list, "exec_id": exec_id},
                dataType: "json",
                success: function (data) {
                    $("#execModal").modal("hide");
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        }else{
            spop({
                template: "请选择服务器",
                style: 'error',
                autoclose: 2000
            });
        }
    });



</script>